1 package uba.db.table.io;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.DataInputStream;
6 import java.io.DataOutputStream;
7
8 import junit.framework.TestCase;
9 import uba.db.column.CharColumnSpecification;
10 import uba.db.column.io.ColumnReader;
11 import uba.db.impl.memory.MemoryTable;
12 import uba.db.table.Row;
13 import uba.db.table.Table;
14 import uba.db.table.TableSchema;
15 import uba.db.table.TableSchemaBuilder;
16
17 /***
18 * @version $Revision: 1.2 $
19 */
20 public class RowWriterTest extends TestCase {
21 private ByteArrayOutputStream out;
22 private RowWriter writer;
23 private CharColumnSpecification colA;
24 private CharColumnSpecification colB;
25 private Table table;
26
27 /***
28 * @see junit.framework.TestCase#setUp()
29 */
30 protected void setUp() throws Exception {
31 super.setUp();
32 colA = new CharColumnSpecification("a", 10);
33 colB = new CharColumnSpecification("b", 5);
34 TableSchema schema = new TableSchemaBuilder("prueba").addColumn(colA).addColumn(colB).build();
35 table = new MemoryTable(schema);
36 out = new ByteArrayOutputStream();
37 writer = new RowWriter(table, new DataOutputStream(out));
38 }
39
40 /***
41 * Test: escribir los valores de una fila.
42 */
43 public void testWrite() throws Exception {
44 String colAValue = "juan";
45 String colBValue = "perez";
46
47 Row row = new Row(table, new Object[] { colAValue, colBValue });
48 writer.write(row);
49
50 DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(
51 out.toByteArray()));
52
53 ColumnReader colAReader = colA.readerFor(dataInputStream);
54 ColumnReader colBReader = colB.readerFor(dataInputStream);
55
56 assertEquals(colAValue, colAReader.read());
57 assertEquals(colBValue, colBReader.read());
58 }
59 }